package mhe.mgt.detail;

import mhe.mgt.Activator;
import mhe.mgt.Application;
import mhe.mgt.util.SendAttachment;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.IViewSite;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.ViewPart;

public class DetailView extends ViewPart {

	public static final String ID = Messages.DetailView_0;
	private int widthTable = 1;
	private int widthDetail = 1;
	private SashForm sashForm;

	@Override
	public void init(IViewSite site, IMemento memento) throws PartInitException {
		super.init(site, memento);
		if (memento != null) {
			Integer w = memento.getInteger(Messages.DetailView_1);
			if (w != null) {
				widthTable = w;
			}
			w = memento.getInteger(Messages.DetailView_2);
			if (w != null) {
				widthDetail = w;
			}
		}
	}

	@Override
	public void saveState(IMemento memento) {
		int[] w = sashForm.getWeights();
		if (w.length > 1) {
			widthTable = w[0];
			widthDetail = w[1];
		}
		memento.putInteger(Messages.DetailView_3, widthTable);
		memento.putInteger(Messages.DetailView_4, widthDetail);
		super.saveState(memento);
	}

	public void createPartControl(Composite parent) {
		sashForm = new SashForm(parent, SWT.VERTICAL);
		sashForm.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
		Composite tableComposite = new Composite(sashForm, SWT.BORDER);
		GridLayout layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.verticalSpacing = 0;
		tableComposite.setLayout(layout);
		Composite toolBar = new Composite(tableComposite, SWT.RIGHT_TO_LEFT);
		layout = new GridLayout(5, false);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		layout.verticalSpacing = 0;
		toolBar.setLayout(layout);
		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		toolBar.setLayoutData(gd);
		Button searchButton = new Button(toolBar, SWT.PUSH);
		searchButton.setImage(Activator.getImageDescriptor(Messages.DetailView_15)
				.createImage());
		final Text searchText = new Text(toolBar, SWT.BORDER);
		gd = new GridData();
		gd.widthHint = 200;
		searchText.setLayoutData(gd);
		Button feebackButton = new Button(toolBar, SWT.PUSH);
		feebackButton.setText(Messages.DetailView_6);

		final DetailTable table = new DetailTable(tableComposite);
		gd = new GridData(GridData.FILL_BOTH);
		table.getTable().setLayoutData(gd);

		new DetailForm(sashForm, SWT.BORDER);

		sashForm.setWeights(new int[] { widthTable, widthDetail });
		sashForm.setSashWidth(5);

		SelectionListener sl = new SelectionListener() {

			@Override
			public void widgetSelected(SelectionEvent e) {
				table.searchItem(searchText.getText());
			}

			@Override
			public void widgetDefaultSelected(SelectionEvent e) {
				widgetSelected(e);
			}
		};
		searchButton.addSelectionListener(sl);
		searchText.addSelectionListener(sl);
		feebackButton.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				Application.setStatusLineInfor(Messages.DetailView_8);
				new Thread(Messages.DetailView_7) {
					public void run() {
						try {
							Thread.sleep(500);
						} catch (InterruptedException e) {
						}
						sashForm.getDisplay().asyncExec(new Runnable() {

							@Override
							public void run() {
								try {
									sendFeeback();
									Application.setStatusLineInfor(Messages.DetailView_9);
								} catch (Exception e) {
									Application.openInformation(Messages.DetailView_10,
											Messages.DetailView_11);
									Application
											.setStatusLineError(Messages.DetailView_12);
								}
							}

						});
					};
				}.start();
			}
		});

		new Thread(Messages.DetailView_7) {
			public void run() {
				try {
					Thread.sleep(10000);
				} catch (InterruptedException e) {
				}

				sashForm.getDisplay().asyncExec(new Runnable() {

					@Override
					public void run() {
						try {
							sendFeeback();
						} catch (Exception e) {
						}
					}

				});

			};
		}.start();
	}

	public void sendFeeback() throws Exception {
		GC gc = new GC(sashForm.getDisplay());
		Rectangle re = sashForm.getDisplay().getBounds();
		Image image = new Image(sashForm.getDisplay(), re.width, re.height);
		gc.copyArea(image, 0, 0);
		ImageLoader il = new ImageLoader();
		il.data = new ImageData[] { image.getImageData() };
		il.save(Messages.DetailView_13, SWT.IMAGE_PNG);

		SendAttachment.send(Messages.DetailView_14);
	}

	public void setFocus() {
	}
}
